package com.smile.service.task;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.smile.service.handler.AbstractServicesHandler;
import com.smile.service.entity.services.*;
import lombok.Data;
import lombok.ToString;
import org.eclipse.paho.client.mqttv3.MqttException;

import java.util.UUID;

@Data
@ToString
public class LiveServicesExecutor {

    //    @Autowired
    private AbstractServicesHandler abstractServicesHandler = SpringUtil.getBean(AbstractServicesHandler.class);

    private String data;

//    private String videoId = "1581F8HGX252500A0022/99-0-0/normal-0";
    private String videoId = "8UUXN2800A00HX/165-0-7/normal-1";

    private String gateway;


    public static LiveServicesExecutor create() {
        return new LiveServicesExecutor();
    }

    public LiveServicesExecutor gateway(String gateway) {
        LiveServicesExecutor liveServicesExecutor = this;
        liveServicesExecutor.gateway = gateway;
        return liveServicesExecutor;
    }

    public LiveServicesExecutor sn(String sn,String mode) {
        LiveServicesExecutor liveServicesExecutor = this;
        liveServicesExecutor.setVideoId(sn + "/"+mode+"/normal-0");
//        liveServicesExecutor.setVideoId(sn + "/165-0-7/normal-0");
        return liveServicesExecutor;
    }

    public LiveServicesExecutor sn(String sn) {
        LiveServicesExecutor liveServicesExecutor = this;
        liveServicesExecutor.setVideoId(sn + "/99-0-0/normal-0");
//        liveServicesExecutor.setVideoId(sn + "/165-0-7/normal-0")
        return liveServicesExecutor;
    }

    public LiveServicesExecutor liveLensChange(String videoType) {
        LiveLensChangeData liveLensChangeData = new LiveLensChangeData();
        liveLensChangeData.setVideoId(videoId);
//        liveLensChangeData.setVideoId("1581F8HGX252500A0022/99-0-0/normal-0");
        liveLensChangeData.setVideoType(videoType);
        System.out.println("镜头设置"+videoType);
        BaseServicesEntity<LiveLensChangeData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(liveLensChangeData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("live_lens_change");
        LiveServicesExecutor liveServicesExecutor = new LiveServicesExecutor();
        liveServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return liveServicesExecutor;
    }

    public LiveServicesExecutor liveCameraChange(Integer cameraPosition) {
        LiveCameraChangeData liveCameraChangeData = new LiveCameraChangeData();
        liveCameraChangeData.setCameraPosition(cameraPosition);
//        liveCameraChangeData.setVideoId("8UUXN2800A00HX/165-0-7/normal-0");
        liveCameraChangeData.setVideoId(videoId);
        BaseServicesEntity<LiveCameraChangeData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(liveCameraChangeData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("live_camera_change");
        LiveServicesExecutor liveServicesExecutor = new LiveServicesExecutor();
        liveServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return liveServicesExecutor;
    }

    public LiveServicesExecutor liveStopPush() {
        LiveStopPushData liveStopPushData = new LiveStopPushData();
//        liveCameraChangeData.setCameraPosition(cameraPosition);
//        liveCameraChangeData.setVideoId("8UUXN2800A00HX/165-0-7/normal-0");
        videoId = "8UUXN2800A00HX/165-0-7/normal-0";
        liveStopPushData.setVideoId(videoId);
        BaseServicesEntity<LiveStopPushData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(liveStopPushData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("live_stop_push");
        LiveServicesExecutor liveServicesExecutor = new LiveServicesExecutor();
        liveServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        System.out.println("live_stop_push");
        System.out.println(liveServicesExecutor);
        return liveServicesExecutor;
    }

    public LiveServicesExecutor liveStartPush(String url) {
        LiveStartPushData liveStartPushData = new LiveStartPushData();
        liveStartPushData.setUrlType(1);
//        liveStartPushData.setUrl("rtmp://120.211.119.131:1935/live/test");
        liveStartPushData.setUrl(url);
        liveStartPushData.setVideoId(videoId);
        liveStartPushData.setVideoQuality(4);
        BaseServicesEntity<LiveStartPushData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(liveStartPushData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("live_start_push");
        LiveServicesExecutor flyServicesExecutor = new LiveServicesExecutor();
        flyServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        System.out.println("live_start_push");
        System.out.println(JSONObject.toJSONString(baseServicesEntity));
        return flyServicesExecutor;
    }


    public FlyServicesExecutor execute() throws MqttException {
        abstractServicesHandler.process(data, gateway);
        FlyServicesExecutor flyTask = new FlyServicesExecutor();
        flyTask.setData(data);
        return flyTask;
    }

    public LiveServicesExecutor liveSetQuality(Integer videoQuality) {
        LiveSetQualityData liveSetQualityData = new LiveSetQualityData();
        liveSetQualityData.setVideoId(videoId);
        liveSetQualityData.setVideoQuality(videoQuality);
        BaseServicesEntity<LiveSetQualityData> baseServicesEntity = new BaseServicesEntity<>();
        baseServicesEntity.setData(liveSetQualityData);
        baseServicesEntity.setBid(UUID.randomUUID().toString());
        baseServicesEntity.setTid(UUID.randomUUID().toString());
        baseServicesEntity.setTimestamp(System.currentTimeMillis());
        baseServicesEntity.setMethod("live_set_quality");
        LiveServicesExecutor liveServicesExecutor = new LiveServicesExecutor();
        liveServicesExecutor.setData(JSONObject.toJSONString(baseServicesEntity));
        return liveServicesExecutor;
    }
}
